﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BISPOS.Repository
{
    public class BillRepository
    {
        BISPOSDataClassesDataContext db = new BISPOSDataClassesDataContext();
        public Bill GetBill(int id) { return db.Bills.FirstOrDefault(x=>x.Id == id);}
        public IEnumerable<Bill> AllBills() { return db.Bills.OrderByDescending(x => x.Date); }
        public IEnumerable<Bill> GetAllLoanBills() { return db.Bills.Where(x => x.IsCredit).OrderByDescending(x => x.Date); }
        public void AddBill(int? supplierId,DateTime date,string note, bool isCredit,List<BillItem> billItems)
        {
            Bill bill = new Bill
            {
                SupplierId = supplierId,
                Date = date,
                Note = note,
                IsCredit = isCredit
            };
            foreach (var billItem in billItems)
            {
                bill.BillItems.Add(new BillItem
                {
                    StockId = billItem.StockId
                });
            }
            db.Bills.InsertOnSubmit(bill);
            db.SubmitChanges();
            
        }
    }
}
